import React, { useState, useEffect } from 'react'
import { Card } from "antd"
import { Table, Tag, Space,Button } from 'antd';
import { getAllMembers,updateMemberToVip,cancleMember} from "../../../http/vip"
export default function index() {
  let [data, setData] = useState([])
  const button1 = async () => {
    const res = await getAllMembers()
    const d = res.data;
    setData(d)
  }
  const delOne = async(_id)=>{
    const res = await cancleMember({_id})
    console.log(res);
     button1()
  }
  const addvip =async (_id,date) => {
    let resDate =""
    let arr = date.split("-") 
    let [nian,yue,ri]= arr.map(item =>item-0)
   if(nian == 0){
    resDate = (new Date().getFullYear()-0) + '-' + (new Date().getMonth() - 0 + 1)  + '-' + new Date().getDate()
    console.log(resDate);
   }else{
    yue +=1
    if(yue >12){
      yue-=12
      nian+=1
    }  
     resDate =(nian+"-"+yue+"-"+ri)+""
     // let resDate = "2021-9-1"
   }
    const res = await updateMemberToVip({ _id,date:resDate})
    button1()
    console.log(res);
  }
  useEffect(() => {
    button1()
  }, [])
  const columns = [
    {
      title: '名字',
      dataIndex: 'nickName',
      render: text => <a>{text}</a>,
    },
    {
      title: '地址',
      dataIndex: 'province',
    },
    {
      title: '会员',
      dataIndex: 'vip',
      render: text => <a>{text ? "是" : "否"}</a>,
    },
    {
      title: '会员剩余时间',
      dataIndex: 'vipdate', 
      render: text => <a>{text ? text : "还不是会员"}</a>,
    },
    {
      title: '头像',
      dataIndex: 'avatarUrl',
      render: imag => <img style={{ width: "50px", height: "50px" }} src={imag}></img>,
    },
    {
      title: '操作',
      dataIndex: '_id',
      render: (text, record) => (
        <Space size="middle">
            <Button type="primary" onClick={() => delOne(record._id)}>
               取消会员
            </Button>
            <Button type="primary" onClick={() => addvip(record._id,record.vipdate)} >
                充值会员
            </Button>
        </Space>
    ),
    },
  ]



  return (

    <Card title="会员管理" extra={<a href="#">More</a>} style={{ width: "98.5% " }}>
      <Table columns={columns} dataSource={data} rowKey="_id" />
    </Card>

  )
}
